    //
//  CustomTabbarView.m
//  Yunho2
//
//  Created by l on 13-6-4.
//
//

#import "CustomTabbarView.h"
#define centerBtnWid 80
#define otherBtnWid 60
#define margin 20

@implementation CustomTabbarView

@synthesize button_1 = _button_1;
@synthesize button_2 = _button_2;

@synthesize button_center = _button_center;
@synthesize delegate;
@synthesize selectedButton;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setFrame:frame];
        [self setBackgroundColor:[UIColor whiteColor]];
        [self layoutView];
    }
    return self;
}

-(void)layoutView
{
    self.button_center = [CustomBarButton buttonWithType:UIButtonTypeCustom];
    _button_center.layer.masksToBounds = YES;
    _button_center.layer.cornerRadius= centerBtnWid/2;
    _button_center.adjustsImageWhenHighlighted = NO;
    [_button_center setImage:[UIImage imageNamed:@"zhuye"] forState:UIControlStateNormal];
    [_button_center setImage:[UIImage imageNamed:@"zhuye_sel"] forState:UIControlStateSelected];
    [_button_center setFrame:CGRectMake((SCREEN_WIDTH-centerBtnWid)/2, -20, centerBtnWid, centerBtnWid)];
    _button_center.tag = 105;
    [_button_center addTarget:self action:@selector(btn1Click:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_button_center];
    
    [self layoutBtn];
    self.button_center.selected = YES;
    self.selectedButton = self.button_center;
    
}

-(void)layoutBtn
{
    CGFloat spaceBtns = (SCREEN_WIDTH - centerBtnWid - otherBtnWid*2 - margin*2)/2;
    
    self.button_1 = [CustomBarButton buttonWithType:UIButtonTypeCustom];
    [_button_1 setFrame:CGRectMake(margin, 0, otherBtnWid, otherBtnWid)];
    [_button_1 setTag:101];
    [_button_1 setImage:[UIImage imageNamed:@"zuixun"] forState:UIControlStateNormal];
    [_button_1 setImage:[UIImage imageNamed:@"zuixun_sel"] forState:UIControlStateSelected];
    
    _button_1.imageEdgeInsets = UIEdgeInsetsMake(5,13,21,_button_center.titleLabel.bounds.size.width);//设置image在button上的位置（上top，左left，下bottom，右right）这里可以写负值，对上写－5，那么image就象上移动5个像素
    [_button_1 setTitle:@"资讯" forState:UIControlStateNormal];
    _button_1.titleLabel.font = [UIFont systemFontOfSize:12];    _button_1.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_button_1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_button_1 setTitleColor:KwBlueColor forState:UIControlStateSelected];
    _button_1.titleEdgeInsets = UIEdgeInsetsMake(30, -_button_1.titleLabel.bounds.size.width-25, 0, 0);//设置title在button上的位置（上top，左left，下bottom，右right）
    
    //    [button setContentEdgeInsets:UIEdgeInsetsMake(70, 0, 0, 0)];//
    _button_1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    
    
    _button_1.contentMode = UIViewContentModeScaleAspectFit;
    _button_1.adjustsImageWhenHighlighted = NO;
    [_button_1 addTarget:self action:@selector(btn1Click:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    self.button_2 = [CustomBarButton buttonWithType:UIButtonTypeCustom];
    [_button_2 setFrame:CGRectMake(_button_center.origin.x+centerBtnWid+spaceBtns, 0,otherBtnWid , otherBtnWid)];
    [_button_2 setTag:102];
    [_button_2 setImage:[UIImage imageNamed:@"wode"] forState:UIControlStateNormal];
    [_button_2 setImage:[UIImage imageNamed:@"wode_sel"] forState:UIControlStateSelected];
    
    _button_2.imageEdgeInsets = UIEdgeInsetsMake(5,13,21,_button_center.titleLabel.bounds.size.width);//设置image在button上的位置（上top，左left，下bottom，右right）这里可以写负值，对上写－5，那么image就象上移动5个像素
    [_button_2 setTitle:@"我的" forState:UIControlStateNormal];
    _button_2.titleLabel.font = [UIFont systemFontOfSize:12];    _button_2.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_button_2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_button_2 setTitleColor:KwBlueColor forState:UIControlStateSelected];
    _button_2.titleEdgeInsets = UIEdgeInsetsMake(30, -_button_2.titleLabel.bounds.size.width-25, 0, 0);//设置title在button上的位置（上top，左left，下bottom，右right）
    
    //    [button setContentEdgeInsets:UIEdgeInsetsMake(70, 0, 0, 0)];//
    _button_2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    
    
    _button_2.adjustsImageWhenHighlighted = NO;
    _button_2.contentMode = UIViewContentModeScaleAspectFit;
    [_button_2 addTarget:self action:@selector(btn1Click:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_button_1];
    [self addSubview:_button_2];
}

-(void)btn1Click:(id)sender
{
    CustomBarButton *btn = (CustomBarButton *)sender;
    if (self.selectedButton != btn) {
        [self.button_1 setSelected:NO];
        [self.button_2 setSelected:NO];
        [self.button_center setSelected:NO];
        NSLog(@"%i",btn.tag);
        self.selectedButton = btn;
        switch (btn.tag) {
            case 101:
            {
                //btn.selected = YES;
                [self.delegate touchBtnAtIndex:0];
                break;
            }
            case 102:
            {
                //btn.selected = YES;
                [self.delegate touchBtnAtIndex:1];
                break;
            }
            case 103:
               // btn.selected = YES;
                [self.delegate touchBtnAtIndex:3];
                break;
            case 104:
                //btn.selected = YES;
                [self.delegate touchBtnAtIndex:4];
                break;
            case 105:
               // btn.selected = YES;
                [self.delegate touchBtnAtIndex:2];
                break;
            default:
                break;
        }
    }
    
}


/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

@end
